package com.hxkj.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hxkj.dao.ArticleDao;
import com.hxkj.dao.ColumnDao;
import com.hxkj.dao.ContentDao;
import com.hxkj.dao.ContentMappingDao;
import com.hxkj.domain.po.*;
import com.hxkj.service.OptionColumnService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName OptionColumnServiceImpl
 * @Description TODO
 * @Author Mr.zheng
 * @Date 2018/6/30 10:28
 * @Version 1.0
 */
@Service
@Slf4j
public class OptionColumnServiceImpl implements OptionColumnService{

    @Autowired
    private ColumnDao columnDao;

    @Autowired
    private ArticleDao articleDao;

    @Autowired
    private ContentDao contentDao;

    @Autowired
    private ContentMappingDao contentMappingDao;

    @Override
    public OptionColumnTree getOptionColumnTree(Column column) throws Exception{
        OptionColumnTree optionColumnTree = new OptionColumnTree();
        try {
            //Mr.zheng:递归调用获取文章分类的树结构
            List<Column> columnList = columnDao.getOptionColumnList(column);
            //Mr.zheng:根据分类ID查出分类，将其分装后递归调用封装
            for (Column auf: columnList) {
                //Mr.zheng:调用封装
                optionColumnTree = addOptionColumnTree(optionColumnTree, auf);
            }

        } catch (Exception e) {
            e.printStackTrace();
            log.info("获取文章分类树及分类下的文章列表失败");
        }
        return optionColumnTree;
    }

    @Override
    public List<OptionColumnTree> getOptionColumnTreeList(Column column) throws Exception{
        List<OptionColumnTree> optionColumnTreeList = new ArrayList<>();
        try {
            optionColumnTreeList = initOptionTree(-1,column);

        } catch (Exception e) {
            e.printStackTrace();
            log.info("获取文章分类树及分类下的文章列表失败");
        }
        return optionColumnTreeList;
    }

    @Override
    public OptionColumnTree getColumnTree(Column column) {
        OptionColumnTree columnTree = new OptionColumnTree();
        try {
            log.debug("分类名称:{},站点id:{},分类信息:{}",column.getColumnName(),column.getSiteId(),column);
            setColumn(column, columnTree);
            if (null != column.getCount() && column.getCount() != -1) {
                setArticleList(column, column, columnTree);
            }
                //Mr.zheng:调用封装
            columnTree.setChildren(initColumnTree(column.getColumnId(), column,column));


        } catch (Exception e) {
            e.printStackTrace();
            log.info("获取文章分类树及分类下的文章列表失败");
        }
        return columnTree;
    }

    private void setColumn(Column column, OptionColumnTree columnTree) {
        columnTree.setColumnName(column.getColumnName() == null ? "" : column.getColumnName());
        columnTree.setColumnId(column.getColumnId());
        columnTree.setParentColumnId(column.getParentColumnId());
        columnTree.setSortNumber(column.getSortNumber());
        columnTree.setComment(column.getComment()==null ? "" : column.getComment());
        columnTree.setFileName1(column.getFileName1()==null ? "" : column.getFileName1());
        columnTree.setImgUrl(column.getImgUrl()==null ? "" : column.getImgUrl());
        columnTree.setTempletName1(column.getTempletName1()==null ? "" : column.getTempletName1());
        columnTree.setPath1(column.getPath1()==null ? "" : column.getPath1());
        columnTree.setFileName2(column.getFileName2()==null ? "" : column.getFileName2());
        columnTree.setTempletName2(column.getTempletName2()==null ? "" : column.getTempletName2());
        columnTree.setPath2(column.getPath2()==null ? "" : column.getPath2());
        columnTree.setSiteId(column.getSiteId());
        columnTree.setCreateTime(column.getCreateTime());
        columnTree.setUpdateTime(column.getUpdateTime());
        columnTree.setTempletId(column.getTempletId());
        columnTree.setTempId(column.getTempId());
        columnTree.setColumnStatus(column.getColumnStatus());
        columnTree.setSeoDepict(column.getSeoDepict());
        columnTree.setSeoHeadline(column.getSeoHeadline());
        columnTree.setSeoKeyword(column.getSeoKeyword());
        columnTree.setManuscriptPath(column.getManuscriptPath());
        columnTree.setColumnType(column.getColumnType());
        columnTree.setPageNum(column.getPageNum());
        columnTree.setPageSize(column.getPageSize());

    }

    private List<OptionColumnTree> initColumnTree(Long columnId, Column column1, Column column) throws Exception{
        List<OptionColumnTree> optionColumnTreeList = new ArrayList<>();
        Column colum = new Column();
        colum.setParentColumnId(columnId);
        colum.setSiteId(column1.getSiteId());
        List<Column> columnList = columnDao.getOptionColumnList(colum);
        if (columnList != null && columnList.size() != 0) {
            for (Column auf : columnList) {
                OptionColumnTree optionColumnTree = new OptionColumnTree();

                setColumnTree(auf, optionColumnTree);
                if (null != column.getCount() && column.getCount() != -1) {
                    setArticleList(column, auf, optionColumnTree);
                }
                //Mr.zheng:递归调用
                optionColumnTree.setChildren(initColumnTree(auf.getColumnId(), auf,column));
                optionColumnTreeList.add(optionColumnTree);

            }
        }
        return optionColumnTreeList;
    }

    private void setArticleList(Column column, Column auf, OptionColumnTree optionColumnTree) {
        List<Column> cols = columnDao.getColumnByParentColumnId(auf.getColumnId());
        Article article = new Article();
        article.setSiteId(auf.getSiteId());
        setColumnIds(auf, article, cols);
        //是否显示图片
        article.setShowImg(column.getChecked());
        article.setCount(column.getCount()== null ? 10 : column.getCount());

        //Mr.zheng:根据分类ID和其他参数条件获取文章列表，如果文章列表不为空，则封装进对象中
        List<Article> articleList = articleDao.getOptionArticleList(article);
        if (articleList.size()>0) {
            if(auf.getContentStatus() == 1) {
                ContentMapping contentMapping = new ContentMapping();
                contentMapping.setColumnId(auf.getColumnId());
                List<ContentMapping> contentMappingList = contentMappingDao.getContentMappingList(contentMapping);
                for (Article artic : articleList) {
                    setContentMap(contentMappingList, artic);
                }
            }
            optionColumnTree.setArticleList(articleList);
        }
    }

    @Async
    protected void setContentMap(List<ContentMapping> contentMappingList, Article artic) {
        Content content = new Content();
        content.setArticleId(artic.getArticleId());
        content.setSiteId(artic.getSiteId());
        List<Content> contentList = contentDao.getContent(content);
        if (contentList != null && contentList.size() > 0){
            content = contentList.get(0);
            String json = JSONObject.toJSONString(content);
            JSONObject jsonObject = JSONObject.parseObject(json);
            Map<String,String> map = new HashMap<>();
            for (ContentMapping Mapping: contentMappingList) {
                map.put(Mapping.getCustomName(), jsonObject.get(Mapping.getContentName()).toString());
            }
            artic.setMap(map);
        }
    }


    private OptionColumnTree addOptionColumnTree(OptionColumnTree optionColumnTree, Column auf) throws Exception {
        setColumn(auf, optionColumnTree);

        //Mr.zheng:递归调用封装该分类下的子分类及其子分类下的文章列表
        List<OptionColumnTree> columnTreeList = initOptionTree(auf.getColumnId(), auf);
        if (columnTreeList.size()>0) {
            optionColumnTree.setChildren(columnTreeList);
        }
        return optionColumnTree;
    }


    private List<OptionColumnTree> initOptionTree(long id, Column col) throws Exception{
        List<OptionColumnTree> optionColumnTreeList = new ArrayList<>();
        Column column = new Column();
        column.setParentColumnId(id);
        column.setSiteId(col.getSiteId());
        List<Column> columnList = columnDao.getOptionColumnList(column);
        if (columnList != null && columnList.size() != 0) {
            for (Column auf : columnList) {
                Article article = new Article();
                OptionColumnTree optionColumnTree = new OptionColumnTree();
                setColumnTree(auf, optionColumnTree);
                article.setSiteId(auf.getSiteId());
                if (auf.getParentColumnId() == -1 && auf.getColumnName().equals("维权中心")) {
                    //Mr.zheng:获取4级推荐的文章集合
                    article.setLocationLevel(4);
                    optionColumnTree.setArticlePush(articleDao.getArticleListByLocationLevel(article));
                }
                //Mr.zheng:根据分类ID和站点ID获取文章列表，如果文章列表不为空，则封装进对象中
                List<Column> cols = columnDao.getColumnByParentColumnId(auf.getColumnId());
                setColumnIds(auf, article, cols);
                //Mr.zheng:分开封装推荐文章和非推荐文章
                List<Article> articleList = articleDao.getOptionArticleList(article);
                if (articleList.size()>0) {
                    optionColumnTree.setArticleList(articleList);
                }
                //Mr.zheng:递归调用
                List<OptionColumnTree> columnTreeList = initOptionTree(auf.getColumnId(), auf);
                if (columnTreeList.size()>0) {
                    optionColumnTree.setChildren(columnTreeList);
                }
                optionColumnTreeList.add(optionColumnTree);

            }
        }
        return optionColumnTreeList;
    }

    private void setColumnIds(Column auf, Article article, List<Column> cols) {
        if (cols.size() > 0){
            StringBuilder ids = new StringBuilder();
            for (Column colu :cols) {
                ids .append(colu.getColumnId()+",");
            }
            article.setColumnIds((ids.deleteCharAt(ids.length() - 1)).toString());
        } else {
            article.setColumnId(auf.getColumnId());
        }
    }

    private void setColumnTree(Column auf, OptionColumnTree optionColumnTree) {
        optionColumnTree.setColumnName(auf.getColumnName() == null ? "" : auf.getColumnName());
        optionColumnTree.setColumnId(auf.getColumnId());
        optionColumnTree.setParentColumnId(auf.getParentColumnId());
        optionColumnTree.setSortNumber(auf.getSortNumber());
        optionColumnTree.setComment(auf.getComment()==null ? "" : auf.getComment());
        optionColumnTree.setFileName1(auf.getFileName1()==null ? "" : auf.getFileName1());
        optionColumnTree.setImgUrl(auf.getImgUrl()==null ? "" : auf.getImgUrl());
        optionColumnTree.setTempletName1(auf.getTempletName1()==null ? "" : auf.getTempletName1());
        optionColumnTree.setPath1(auf.getPath1()==null ? "" : auf.getPath1());
        optionColumnTree.setFileName2(auf.getFileName2()==null ? "" : auf.getFileName2());
        optionColumnTree.setTempletName2(auf.getTempletName2()==null ? "" : auf.getTempletName2());
        optionColumnTree.setPath2(auf.getPath2()==null ? "" : auf.getPath2());
        optionColumnTree.setSiteId(auf.getSiteId());
        optionColumnTree.setCreateTime(auf.getCreateTime());
        optionColumnTree.setUpdateTime(auf.getUpdateTime());
        optionColumnTree.setTempletId(auf.getTempletId());
        optionColumnTree.setTempId(auf.getTempId());
        optionColumnTree.setColumnStatus(auf.getColumnStatus());
        optionColumnTree.setSeoDepict(auf.getSeoDepict());
        optionColumnTree.setSeoHeadline(auf.getSeoHeadline());
        optionColumnTree.setSeoKeyword(auf.getSeoKeyword());
        optionColumnTree.setColumnType(auf.getColumnType());
        optionColumnTree.setManuscriptPath(auf.getManuscriptPath());
        optionColumnTree.setPageNum(auf.getPageNum());
        optionColumnTree.setPageSize(auf.getPageSize());
    }
}


